package v1

import (
	"fmt"
	"ginblog/utils"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
	"time"
)

func UploadFile(c *gin.Context)  {

	file, _ := c.FormFile("file")

	//获取当前项目根路径
	dir, _ := os.Getwd()

	//上传文件一定要是绝对路径并包含文件名称
	uploadKey := time.Now().Format("20060102030405") + ".jpg"
	dst := dir + fmt.Sprintf("\\upload\\%s", uploadKey)
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		c.String(http.StatusOK, fmt.Sprintf("uploaded error : '%s'!", err))
		return
	}
	filePath, code := utils.UploadFile(dst, uploadKey)

	c.JSON(http.StatusOK, gin.H{
		"status" : code,
		"message" : errmsg.GetErrMsg(code),
		"file_path" : filePath,
	})

}
